﻿using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System;
using System.ComponentModel;

namespace PictureSearch.AppCode
{
    public static class FileCommon
    {
        public static void CreateDirectory(string directoryName)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!iso.DirectoryExists(directoryName))
                {
                    iso.CreateDirectory(directoryName);
                }
            }
        }

        public static bool IsFileExist(string filePath)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return iso.FileExists(filePath);
            }
        }

        public static bool IsDirectoryExist(string directoryPath)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return iso.DirectoryExists(directoryPath);
            }
        }

        public static void DeleteFile(string filePath)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.FileExists(filePath))
                {
                    iso.DeleteFile(filePath);
                }
            }
        }

        public static void DeleteDirectory(string directoryPath)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.DirectoryExists(directoryPath))
                {
                    iso.DeleteDirectory(directoryPath);
                }
            }
        }

        public static string[] GetFilesInDirectory(string directoryPath)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.DirectoryExists(directoryPath))
                {
                    string searchpath = string.Format("{0}\\*.*", directoryPath);
                    return iso.GetFileNames(searchpath);
                }
            }

            return null;
        }

        public static void SaveImage(BitmapImage image, string fileName)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.FileExists(fileName))
                {
                    iso.DeleteFile(fileName);
                }

                using (IsolatedStorageFileStream isostream = iso.CreateFile(fileName))
                {
                    WriteableBitmap wb = new WriteableBitmap(image);
                    wb.SaveJpeg(isostream, wb.PixelWidth, wb.PixelHeight, 0, 100);
                    isostream.Close();
                }
            }
        }

        public static void LoadImage(string fileName, Action<MemoryStream> action)
        {
            var bk = new BackgroundWorker();
            bk.DoWork += (sender, e)=>
            {
                var data = new MemoryStream();

                using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var isostream = iso.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                    {
                        isostream.CopyTo(data);
                        data.Seek(0, SeekOrigin.Begin);
                    }
                }

                e.Result = data;
            };

            bk.RunWorkerCompleted += (sender, e) =>
            {
                if (e.Cancelled || e.Error != null)
                {
                    return;
                }

                var data = e.Result as MemoryStream;

                if (action != null && data != null)
                {
                    action(data);
                }
            };

            bk.RunWorkerAsync();
        }
    }
}
